#
#  Copyright 2008 Garth Williams
#
#  This file is part of CommandPost.
#
#  CommandPost is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  CommandPost is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with CommandPost.  If not, see <http://www.gnu.org/licenses/>.
#

class PathsController < ApplicationController
  
  # GET /pages/new
  # GET /path/new.xml
  def new
    @path = Path.new
    @stage = Stage.find(params[:stage_id])
    @report_type = @stage.report_type
    @path.from_stage = @stage
    @next_stages = @report_type.stages.find(:all, :conditions => ['id != ?', @stage.id])

    respond_to do |format|
      format.html { render :layout => false }# new.html.erb
      format.xml  { render :xml => @stage }
    end
  end
  
  # GET /paths/1/edit
  def edit
    @path = Path.find(params[:id])
    @stage = @path.from_stage
    @report_type = @stage.report_type
    @next_stages = @report_type.stages.find(:all, :conditions => ['id != ?', @stage.id])
    @roles = Role.find(:all, :order => 'name')
    
    render :layout => false
  end

  # POST /paths
  # POST /paths.xml
  def create
    @path = Path.new(params[:path])
    @stage = Stage.find(params[:stage_id])
    @report_type = @stage.report_type
    @path.created_by_user_id = session[:user_id]
    @path.from_stage = @stage
    @roles = Role.find(:all, :order => 'name')

    respond_to do |format|
      if @path.save
        flash[:notice] = 'Path was successfully created.'
        format.html { render :template => 'stages/edit', :layout => false }
        format.xml  { render :xml => @path, :status => :created, :location => @path }
      else
        format.html { render :action => "new", :layout => false, :status => 500 }
        format.xml  { render :xml => @path.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /stages/1
  # PUT /stages/1.xml
  def update
    @path = Path.find(params[:id])
    @stage = @path.from_stage
    @report_type = @stage.report_type
    @path.created_by_user_id = session[:user_id]
    @roles = Role.find(:all, :order => 'name')

    respond_to do |format|
      if @path.update_attributes(params[:path])
        flash[:notice] = 'Path was successfully updated.'
        format.html { render :template => 'stages/edit', :layout => false }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit", :layout => false, :status => 500 }
        format.xml  { render :xml => @path.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # DELETE /paths/1
  # DELETE /paths/1.xml
  def destroy
    @path = Path.find(params[:id])
    @stage = @path.from_stage
    @report_type = @stage.report_type
    @path.created_by_user_id = session[:user_id]
    @path.destroy
    @roles = Role.find(:all, :order => 'name')

    respond_to do |format|
      format.html { render :template => 'stages/edit', :layout => false }
      format.xml  { head :ok }
    end
  end
end
